package com.urban.pooling.controller;

import java.util.Date;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import thymeleaf.spring.support.Layout;

import com.urban.pooling.domain.GeoPoint;
import com.urban.pooling.service.RouteService;

/**
 * Handles requests for the pages involving routes.
 */
@Layout(value="layouts/sidebar_layout.html")
@Controller
@RequestMapping(value="/route")
public class RouteController {
	private static final Logger logger = LoggerFactory
			.getLogger(RouteController.class);
	
	@Autowired
	private RouteService routeService;

	/**
	 * Serves the page for adding a new route.
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addRoutePage() {
		logger.info("GET: add route page");
		return "/routes/add_route.html";
	}
	
	/**
	 * Adds a new route.
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addRoute(
			@RequestParam(value="startLabel")String startLabel,
			@RequestParam(value="destLabel") String destLabel,
			@RequestParam(value="car") String car, 
			@RequestParam(value="seats") int seats,
			@RequestParam(value="startTime") Date startTime,
			@RequestParam(value="arrivalTime") String arrivalTime,
			@RequestParam(value="returns") Boolean returns,
			@RequestParam(value="returnTime") String returnTime,
			@RequestParam(value="returnArrivalTime") String returnArrivalTime,
			@RequestParam(value="") Set<GeoPoint> geoPoints,
			@RequestParam(value="") Boolean[] schedule) {
		logger.info("POST: add route");
		
		return "routes/route_list.html";
	}
	
	/**
	 * Serves the page for viewing the route list.
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getRouteListPage(){
		return "routes/route_list.html";
	}
	
	/**
	 * Serves the page for updating a route.
	 */
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String updateRoutePage(){
		return "routes/update_route.html";
	}
	
	/**
	 * Serves the page for updating a route.
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateRoute(){
		return "routes/update_route.html";
	}
	
	/**
	 * Deletes a route by id.
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String deleteRoute(@RequestParam Integer id){
		routeService.deleteRoute(id);
		return "routes/route_list.html";
	}
	
}
